﻿using System;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using HYElectricKernel;
using HYRElectric.WirePlace;

namespace HYRElectric.Equipment
{
	public class RectLayout : EquipmentLayout
	{
		public RectLayout(UIDocument uiDoc, Document document, DbFamilyObject familyObject, FamilySymbol selectedFamilySymbol, List<BuiltInCategory> lstFilter) : base(uiDoc, document, familyObject, selectedFamilySymbol, lstFilter)
		{
		}

		public override bool Layout()
		{
			if (!base.Layout())
			{
				return false;
			}
			this.form = new RectLayoutForm(this.doc);
			this.form.Show(WindowHelper.HWndRevit);
			WindowHelper.SendMessageToWndRevit();
			for (;;)
			{
				try
				{
					this.basePoint = this.docUI.Selection.PickPoint("请选取一点");
					LayoutEquipmentJig layoutEquipmentJig = new LayoutEquipmentJig(this.docUI, this.form);
					this.diagonalPoint = layoutEquipmentJig.PickEndPoint(this.basePoint, LayoutEquipmentJig.ELayoutType.eRect, "请选取对角点");
					layoutEquipmentJig.Dispose();
					if (this.diagonalPoint == null)
					{
						break;
					}
					if ((this.diagonalPoint - this.basePoint).IsZeroLength())
					{
						TaskDialog.Show("盈建科提示", "选取的两点为同一点，请选取两个不同点。");
						continue;
					}
				}
				catch (Exception)
				{
					break;
				}
				List<List<XYZ>> insertPoints = this.GetInsertPoints();
				if (!this.IsEmptyPointSet(insertPoints))
				{
					double angle = Convert.ToDouble(this.form.Angle);
					TransactionGroup transactionGroup = new TransactionGroup(this.doc, "TransactionGroup");
					try
					{
						transactionGroup.Start();
						Transaction transaction = null;
						try
						{
							foreach (List<XYZ> list in insertPoints)
							{
								FamilyInstance startdevice = null;
								foreach (XYZ xyz in list)
								{
									transaction = new Transaction(this.doc, "RectLayout");
									transaction.Start();
									FamilyInstance familyInstance = YJKRevitTools.LayFamilySymbol(this.doc, xyz, this.familySymbol, this.doc.ActiveView.GenLevel, angle);
									if (familyInstance == null)
									{
										throw new Exception("布置设备出错。");
									}
									base.SetParametersValue(familyInstance, this.dbFamilyObject.Size);
									transaction.Commit();
									if (this.form.LinkLineWay != 0)
									{
                                        NewConnectWire.CreateWire(this.doc, startdevice, familyInstance, (Autodesk.Revit.DB.Electrical.WiringType)this.form.WireStyle, null);
										startdevice = familyInstance;
									}
								}
							}
						}
						catch (Exception)
						{
							if (transaction != null && transaction.GetStatus() != TransactionStatus.Committed)
							{
								transaction.RollBack();
							}
							break;
						}
						transactionGroup.Assimilate();
						continue;
					}
					catch (Exception)
					{
						transactionGroup.RollBack();
						continue;
					}
					break;
				}
			}
			this.form.Close();
			return true;
		}

		private bool IsEmptyPointSet(List<List<XYZ>> pointSet)
		{
			if (pointSet == null || pointSet.Count <= 0)
			{
				return true;
			}
			foreach (List<XYZ> list in pointSet)
			{
				if (list != null && list.Count > 0)
				{
					return false;
				}
			}
			return true;
		}

		private List<List<XYZ>> GetInsertPoints()
		{
			if (this.form == null)
			{
				return null;
			}
			List<List<XYZ>> list = new List<List<XYZ>>();
			this.xSpace = UnitConvert.LengthToStd(Convert.ToDouble(this.form.ColSpace) / 1000.0);
			this.ySpace = UnitConvert.LengthToStd(Convert.ToDouble(this.form.RowSpace) / 1000.0);
			this.xMargin = UnitConvert.LengthToStd(Convert.ToDouble(this.form.ColMargin) / 1000.0);
			this.yMargin = UnitConvert.LengthToStd(Convert.ToDouble(this.form.RowMargin) / 1000.0);
			int rowAngle = this.form.RowAngle;
			if (this.xSpace <= 0.0 || this.ySpace <= 0.0)
			{
				return null;
			}
			double num = AssistFunc.AngleToRadian((double)rowAngle);
			XYZ xyz = Geometry.RotateTo(this.diagonalPoint, this.basePoint, XYZ.BasisZ, -num);
			this.xDiagonalPoint = new XYZ(xyz.X, this.basePoint.Y, xyz.Z);
			this.yDiagonalPoint = new XYZ(this.basePoint.X, xyz.Y, xyz.Z);
			this.xLength = this.basePoint.DistanceTo(this.xDiagonalPoint);
			this.yLength = this.basePoint.DistanceTo(this.yDiagonalPoint);
			this.xDist = this.xMargin;
			this.yDist = this.yMargin;
			if (this.form.DislocationWay == 2)
			{
				list = this.GetInsertPointsOfCol();
			}
			else if (this.form.DislocationWay == 1)
			{
				list = this.GetInsertPointsOfRow();
			}
			else
			{
				list = this.GetInsertPointsOfNone();
			}
			foreach (List<XYZ> list2 in list)
			{
				for (int i = 0; i < list2.Count; i++)
				{
					XYZ pt = list2[i];
					list2[i] = Geometry.RotateTo(pt, this.basePoint, XYZ.BasisZ, num);
				}
			}
			return list;
		}

		private List<List<XYZ>> GetInsertPointsOfNone()
		{
			List<List<XYZ>> list = new List<List<XYZ>>();
			if (this.form.LinkLineWay == 2)
			{
				while (this.xDist < this.xLength)
				{
					List<XYZ> list2 = new List<XYZ>();
					this.yDist = this.yMargin;
					XYZ xyz = this.basePoint + this.xDist * (this.xDiagonalPoint - this.basePoint).Normalize();
					while (this.yDist < this.yLength)
					{
						XYZ item = xyz + this.yDist * (this.yDiagonalPoint - this.basePoint).Normalize();
						this.yDist += this.ySpace;
						list2.Add(item);
					}
					this.xDist += this.xSpace;
					list.Add(list2);
				}
			}
			else
			{
				while (this.yDist < this.yLength)
				{
					List<XYZ> list3 = new List<XYZ>();
					this.xDist = this.xMargin;
					XYZ xyz2 = this.basePoint + this.yDist * (this.yDiagonalPoint - this.basePoint).Normalize();
					while (this.xDist < this.xLength)
					{
						XYZ item2 = xyz2 + this.xDist * (this.xDiagonalPoint - this.basePoint).Normalize();
						this.xDist += this.xSpace;
						list3.Add(item2);
					}
					this.yDist += this.ySpace;
					list.Add(list3);
				}
			}
			return list;
		}

		private List<List<XYZ>> GetInsertPointsOfRow()
		{
			List<List<XYZ>> list = new List<List<XYZ>>();
			int num = 1;
			if (this.form.LinkLineWay == 2)
			{
				while (this.xDist < this.xLength)
				{
					List<XYZ> list2 = new List<XYZ>();
					this.yDist = (AssistFunc.IsOdd(num) ? this.yMargin : (this.yMargin + this.ySpace));
					XYZ xyz = this.basePoint + this.xDist * (this.xDiagonalPoint - this.basePoint).Normalize();
					while (this.yDist < this.yLength)
					{
						XYZ item = xyz + this.yDist * (this.yDiagonalPoint - this.basePoint).Normalize();
						this.yDist += this.ySpace * 2.0;
						list2.Add(item);
					}
					num++;
					this.xDist += this.xSpace / 2.0;
					list.Add(list2);
				}
			}
			else
			{
				while (this.yDist < this.yLength)
				{
					List<XYZ> list3 = new List<XYZ>();
					this.xDist = (AssistFunc.IsOdd(num) ? this.xMargin : (this.xMargin + this.xSpace / 2.0));
					XYZ xyz2 = this.basePoint + this.yDist * (this.yDiagonalPoint - this.basePoint).Normalize();
					while (this.xDist < this.xLength)
					{
						XYZ item2 = xyz2 + this.xDist * (this.xDiagonalPoint - this.basePoint).Normalize();
						this.xDist += this.xSpace;
						list3.Add(item2);
					}
					num++;
					this.yDist += this.ySpace;
					list.Add(list3);
				}
			}
			return list;
		}

		private List<List<XYZ>> GetInsertPointsOfCol()
		{
			List<List<XYZ>> list = new List<List<XYZ>>();
			int num = 1;
			if (this.form.LinkLineWay == 2)
			{
				while (this.xDist < this.xLength)
				{
					List<XYZ> list2 = new List<XYZ>();
					this.yDist = (AssistFunc.IsOdd(num) ? this.yMargin : (this.yMargin + this.ySpace / 2.0));
					XYZ xyz = this.basePoint + this.xDist * (this.xDiagonalPoint - this.basePoint).Normalize();
					while (this.yDist < this.yLength)
					{
						XYZ item = xyz + this.yDist * (this.yDiagonalPoint - this.basePoint).Normalize();
						this.yDist += this.ySpace;
						list2.Add(item);
					}
					num++;
					this.xDist += this.xSpace;
					list.Add(list2);
				}
			}
			else
			{
				while (this.yDist < this.yLength)
				{
					List<XYZ> list3 = new List<XYZ>();
					this.xDist = (AssistFunc.IsOdd(num) ? this.xMargin : (this.xMargin + this.xSpace));
					XYZ xyz2 = this.basePoint + this.yDist * (this.yDiagonalPoint - this.basePoint).Normalize();
					while (this.xDist < this.xLength)
					{
						XYZ item2 = xyz2 + this.xDist * (this.xDiagonalPoint - this.basePoint).Normalize();
						this.xDist += this.xSpace * 2.0;
						list3.Add(item2);
					}
					num++;
					this.yDist += this.ySpace / 2.0;
					list.Add(list3);
				}
			}
			return list;
		}

		private RectLayoutForm form;

		private XYZ basePoint;

		private XYZ diagonalPoint;

		private double xSpace;

		private double ySpace;

		private double xMargin;

		private double yMargin;

		private XYZ xDiagonalPoint = XYZ.Zero;

		private XYZ yDiagonalPoint = XYZ.Zero;

		private double xLength;

		private double yLength;

		private double xDist;

		private double yDist;
	}
}
